<?php

class KaryawanController extends CController
{
        public $breadcrumbs;
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='adtheme';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','popup'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','GeneratePdf','GenerateExcel'),
				'users'=>array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$criteria=new CDbCriteria;
		$criteria->alias = 'k';
        $criteria->select = "k.*,
							a.agama,
							j.nama_jabatan,
							ty.nama_type_dinas,
							CASE 
								WHEN k.jenis_kelamin = 1 THEN 'Laki-Laki'
								ELSE 'Perempuan'
							END AS jenis_kelamin,
							CASE 
								WHEN k.stat_pernikahan = 1 THEN 'Menikah'
								ELSE 'Belum Menikah'
							END AS stat_pernikahan"; 
		$criteria->join = ' left join type_dinas ty on ty.id_type_dinas = k.id_type_dinas';
		$criteria->join .= ' left join agama a on a.id_agama = k.id_agama';
		$criteria->join .= ' left join jabatan j on j.id_jabatan = k.id_jabatan';
		$criteria->condition.= " k.id_kary='$id'";
		$model=Karyawan::model()->find($criteria);
		$this->render('view',array(
			'model'=>$model,
		));
	}
	
	public function actionPopup()
	{
		Yii::app()->clientScript->scriptMap['jquery.js'] = false;
		$this->layout = false;
		$model=new Karyawan('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Karyawan']))
			$model->attributes=$_GET['Karyawan'];
		$this->render('winKaryawan',array(
			'model'=>$model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Karyawan;

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model);

		if(isset($_POST['Karyawan']))
		{
			$model->attributes=$_POST['Karyawan'];
			if($model->tgl_lahir!='')$model->tgl_lahir=Yii::app()->spd->dateFormating($model->tgl_lahir); 
			if($model->tgl_mulai_kerja!='')$model->tgl_mulai_kerja=Yii::app()->spd->dateFormating($model->tgl_mulai_kerja);else $model->tgl_mulai_kerja=null; 
			if($model->tg_keluar_kerja!='')$model->tg_keluar_kerja=Yii::app()->spd->dateFormating($model->tg_keluar_kerja);else $model->tg_keluar_kerja=null;
			
			if($model->save())
				$this->redirect(array('view','id'=>$model->id_kary));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Karyawan']))
		{
			$model->attributes=$_POST['Karyawan'];
			if($model->tgl_lahir!='')$model->tgl_lahir=Yii::app()->spd->dateFormating($model->tgl_lahir); 
			if($model->tgl_mulai_kerja!='')$model->tgl_mulai_kerja=Yii::app()->spd->dateFormating($model->tgl_mulai_kerja);else $model->tgl_mulai_kerja=null; 
			if($model->tg_keluar_kerja!='')$model->tg_keluar_kerja=Yii::app()->spd->dateFormating($model->tg_keluar_kerja);else $model->tg_keluar_kerja=null;
			
			if($model->save())
				$this->redirect(array('view','id'=>$model->id_kary));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
            $session=new CHttpSession;
            $session->open();		
            $criteria = new CDbCriteria();            

                $model=new Karyawan('search');
                $model->unsetAttributes();  // clear any default values

                if(isset($_GET['Karyawan']))
		{
                        $model->attributes=$_GET['Karyawan'];
			
			
                   	
                       if (!empty($model->id_kary)) $criteria->addCondition('id_kary = "'.$model->id_kary.'"');
                     
                    	
                       if (!empty($model->id_type_dinas)) $criteria->addCondition('id_type_dinas = "'.$model->id_type_dinas.'"');
                     
                    	
                       if (!empty($model->id_agama)) $criteria->addCondition('id_agama = "'.$model->id_agama.'"');
                     
                    	
                       if (!empty($model->id_jabatan)) $criteria->addCondition('id_jabatan = "'.$model->id_jabatan.'"');
                     
                    	
                       if (!empty($model->nip)) $criteria->addCondition('nip = "'.$model->nip.'"');
                     
                    	
                       if (!empty($model->nama)) $criteria->addCondition('nama = "'.$model->nama.'"');
                     
                    	
                       if (!empty($model->tgl_lahir)) $criteria->addCondition('tgl_lahir = "'.$model->tgl_lahir.'"');
                     
                    	
                       if (!empty($model->tempat_lahir)) $criteria->addCondition('tempat_lahir = "'.$model->tempat_lahir.'"');
                     
                    	
                       if (!empty($model->jenis_kelamin)) $criteria->addCondition('jenis_kelamin = "'.$model->jenis_kelamin.'"');
                     
                    	
                       if (!empty($model->stat_pernikahan)) $criteria->addCondition('stat_pernikahan = "'.$model->stat_pernikahan.'"');
                     
                    	
                       if (!empty($model->alamat)) $criteria->addCondition('alamat = "'.$model->alamat.'"');
                     
                    	
                       if (!empty($model->no_telp)) $criteria->addCondition('no_telp = "'.$model->no_telp.'"');
                     
                    	
                       if (!empty($model->npwp)) $criteria->addCondition('npwp = "'.$model->npwp.'"');
                     
                    	
                       if (!empty($model->pen_terakhir)) $criteria->addCondition('pen_terakhir = "'.$model->pen_terakhir.'"');
                     
                    	
                       if (!empty($model->tgl_mulai_kerja)) $criteria->addCondition('tgl_mulai_kerja = "'.$model->tgl_mulai_kerja.'"');
                     
                    	
                       if (!empty($model->tg_keluar_kerja)) $criteria->addCondition('tg_keluar_kerja = "'.$model->tg_keluar_kerja.'"');
                     
                    			
		}
                 $session['Karyawan_records']=Karyawan::model()->findAll($criteria); 
       

                $this->render('index',array(
			'model'=>$model,
		));

	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Karyawan('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Karyawan']))
			$model->attributes=$_GET['Karyawan'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Karyawan::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='karyawan-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        public function actionGenerateExcel()
	{
            $session=new CHttpSession;
            $session->open();		
            
             if(isset($session['Karyawan_records']))
               {
                $model=$session['Karyawan_records'];
               }
               else
                 $model = Karyawan::model()->findAll();

		
		Yii::app()->request->sendFile(date('YmdHis').'.xls',
			$this->renderPartial('excelReport', array(
				'model'=>$model
			), true)
		);
	}
        public function actionGeneratePdf() 
	{
           $session=new CHttpSession;
           $session->open();
		Yii::import('application.modules.admin.extensions.giiplus.bootstrap.*');
		require_once('tcpdf/tcpdf.php');
		require_once('tcpdf/config/lang/eng.php');

             if(isset($session['Karyawan_records']))
               {
                $model=$session['Karyawan_records'];
               }
               else
                 $model = Karyawan::model()->findAll();



		$html = $this->renderPartial('expenseGridtoReport', array(
			'model'=>$model
		), true);
		
		//die($html);
		
		$pdf = new TCPDF();
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor(Yii::app()->name);
		$pdf->SetTitle('Karyawan Report');
		$pdf->SetSubject('Karyawan Report');
		//$pdf->SetKeywords('example, text, report');
		$pdf->SetHeaderData('', 0, "Report", '');
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "Example Report by ".Yii::app()->name, "");
		$pdf->setHeaderFont(Array('helvetica', '', 8));
		$pdf->setFooterFont(Array('helvetica', '', 6));
		$pdf->SetMargins(15, 18, 15);
		$pdf->SetHeaderMargin(5);
		$pdf->SetFooterMargin(10);
		$pdf->SetAutoPageBreak(TRUE, 0);
		$pdf->SetFont('dejavusans', '', 7);
		$pdf->AddPage();
		$pdf->writeHTML($html, true, false, true, false, '');
		$pdf->LastPage();
		$pdf->Output("Karyawan_002.pdf", "I");
	}
}
